import 'package:dio/dio.dart';
import 'package:erp/data/model/process_file.dart';
import 'package:erp/util/app_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:open_file/open_file.dart';
import 'package:permission_handler/permission_handler.dart';

class ProcessAttachmentList extends StatelessWidget {
  final List<ProcessFile> processFileList;

  ProcessAttachmentList({Key? key, required this.processFileList});

  @override
  Widget build(BuildContext context) {
    var files = processFileList.asMap().entries.map((entry) {
      ProcessFile file = entry.value;
      String url = file.fileUrl ?? "";
      var button = Material(
        child: Ink(
          decoration: BoxDecoration(
            color: Color(0xff4C88FF),
            borderRadius: BorderRadius.all(
              Radius.circular(4.r),
            ),
          ),
          child: InkWell(
            onTap: () async {
              if (await Permission.storage.request().isGranted) {
                // 安卓
                if (url.isNotEmpty) {
                  // 安卓10以上 区分存储
                  var path = await AppUtils.findLocalPath() + '/Download';
                  String targetName = path + '/' + file.fileName!;
                  try {
                    EasyLoading.show(status: '下载文件中...');
                    Response response = await Dio(BaseOptions(connectTimeout: 10000)).download(url, targetName);
                    await Future.delayed(Duration(seconds: 1));
                    if (response.statusCode == 200) {
                      EasyLoading.dismiss();
                      AppUtils.confirm(
                        content: "下载完成，是否打开文件？",
                        confirmCallback: () {
                          OpenFile.open(targetName);
                        },
                      );
                    }
                  } catch (e) {
                    AppUtils.confirm(content: e.toString());
                    EasyLoading.showError('下载失败');
                  } finally {
                    EasyLoading.dismiss();
                  }
                }
              }
            },
            child: Container(
              alignment: AlignmentDirectional.center,
              width: 60.w,
              height: 30.h,
              child: Text(
                '下载',
                style: TextStyle(color: Colors.white, fontSize: 16.sp),
              ),
            ),
          ),
        ),
      );

      return Padding(
        padding: EdgeInsets.only(top: 20.h),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Container(
              child: Row(
                children: [
                  Image.asset(
                    'assets/images/zip.png',
                    width: 20.w,
                    height: 20.h,
                  ),
                  SizedBox(width: 10.w),
                  Text(
                    entry.value.fileName ?? '',
                    style: TextStyle(color: Color(0xFF555555), fontSize: 14.sp),
                  ),
                ],
              ),
            ),
            button
          ],
        ),
      );
    }).toList();
    return Column(
      children: files,
    );
  }
}
